﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using EntityFrameworks.Data.Core;
using EntityFrameworks.Entity.Core;
using IRepository.Core;

namespace Common.Background.BLL
{
    public class UserManager : BLLBase
    {
        /// <summary>
        /// 调用BLL层的其它对象
        /// </summary>
        MenuManager menuManger;
        public UserManager(IUnitOfWork iUnitOfWork)
            : base(iUnitOfWork)
        {
            menuManger = new MenuManager(iUnitOfWork);//保证menuManger和userManager使用同一个数据上下文
        }
        public UserManager()
            : this(null)
        {

        }

        /// <summary>
        /// 得到完整的用户信息
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public PagedList<WebManageUsers> GetWebManageUsers(
            Expression<Func<WebManageUsers, bool>> predicate,
            PageParameters pp)
        {
            return LoadRepository<WebManageUsers>().GetModel()
                                                   .Include(i => i.WebDepartments)
                                                   .Include(i => i.WebManageRoles)
                                                   .Where(predicate)
                                                   .OrderByDescending(i => i.ManageUserID)
                                                   .ToPagedList(pp.PageIndex, pp.PageSize);
        }

        /// <summary>
        /// 通过用户列表规约，得到用户对象
        /// </summary>
        /// <param name="userSpecification"></param>
        /// <returns></returns>
        public PagedList<WebManageUsers> GetWebManageUsers(
            UserSpecification userSpecification,
            PageParameters pp)
        {
            return LoadRepository<WebManageUsers>().GetModel(userSpecification)
                                                   .Include(i => i.WebDepartments)
                                                   .Include(i => i.WebManageRoles)
                                                   .OrderByDescending(i => i.ManageUserID)
                                                   .ToPagedList(pp.PageIndex, pp.PageSize);
        }

        public VMessage AddUser(WebManageUsers entity)
        {
            try
            {
                Message = new VMessage();
                LoadRepository<WebManageUsers>().Insert(entity);
            }
            catch (Exception ex)
            {
                Message.AddItem(ex.Message);
            }
            return Message;
        }

        public VMessage ModifyUser(WebManageUsers entity)
        {
            try
            {
                Message = new VMessage();
                LoadRepository<WebManageUsers>().Update(entity);
            }
            catch (Exception ex)
            {
                Message.AddItem(ex.Message);
            }
            return Message;
        }

        public WebDepartments GetDept(int deptID)
        {
            return LoadRepository<WebDepartments>().Find(deptID);
        }

        public WebManageRoles GetRole(int roleID)
        {
            return LoadRepository<WebManageRoles>().Find(roleID);
        }

    }
}